import requests
import json

def get_bearer_token():
    # 从authToken.txt文件中读取bearer_token的值
    with open("./file/authToken.txt", "r") as file:
        return file.read().strip()

def sendHttps(urlName, method, body=None):
    url = f"https://wechat.17donor.com/weixin/{urlName}"
    
    # 获取bearer_token
    bearer_token = get_bearer_token()
    
    if method == "GET":
        headers = {
            "Authorization": bearer_token
        }
        response = requests.get(url, headers=headers)
    elif method == "POST":
        headers = {
            "Authorization": bearer_token,
            "Content-Type": "application/json"  # 设置请求体的Content-Type
        }
        response = requests.post(url, headers=headers, data=json.dumps(body))
    else:
        return "不支持的HTTP方法"
    
    return response.json()

# 检测隐私接口
def getCodePrivacyInfo(appId):
    urlName = f"/wxSetPrivacy/getCodePrivacyInfo/{appId}";
    return sendHttps(urlName, "GET")

# 提交代码
def codeCommit(appId, templateId=5):
    urlName = f"/wxMPVersion/codeCommit/{templateId}/{appId}";
    return sendHttps(urlName, "POST")
    
# 提交审核
def submitAudit(appId, templateId=5):
    urlName = f"/wxMPVersion/submitAudit/{templateId}/{appId}";
    return sendHttps(urlName, "POST")

# 小程序发布版本
def release(appId):
    urlName = f"/wxMPVersion/release/{appId}";
    return sendHttps(urlName, "POST")
    
# 查询最新一次提交的审核状态
def getLastAuditStatus(appId):
    urlName = f"/wxSetPrivacy/getLastAuditStatus/{appId}";
    return sendHttps(urlName, "GET")

# 查询小程序用户隐私保护指引
def getPrivacySetting(appId):
    urlName = f"/wxSetPrivacy/getPrivacySetting/{appId}";
    return sendHttps(urlName, "GET")

# 配置小程序用户隐私保护指引
def setting(appId):
    urlName = f"/wxSetPrivacy/setting/{appId}";
    body = {
        "owner_setting": {
            "contact_email": "zhangwh@sharing8.cn",
            "notice_method": "通过弹窗方式",
            "store_expire_timestamp": "180天"
        },
        "setting_list": [
            {
                "privacy_key": "Location",
                "privacy_text": "获取位置信息来定位当前所在城市，自动匹配对应血站；以及计算与各个献血点的距离、献血点导航等"
            },
            {
                "privacy_key": "getLocation",
                "privacy_text": "获取位置信息来定位当前所在城市，自动匹配对应血站；以及计算与各个献血点的距离、献血点导航等"
            },
            {
                "privacy_key": "Record",
                "privacy_text": "麦克风获取原因"
            },
            {
                "privacy_key": "Album",
                "privacy_text": "设置头像等信息时读取本地相册"
            },
            {
                "privacy_key": "Camera",
                "privacy_text": "人脸识别认证"
            },
            {
                "privacy_key": "PhoneNumber",
                "privacy_text": "手机号授权登录"
            },
            {
                "privacy_key": "UserInfo",
                "privacy_text": "自动一键授权注册，设置微信头像为献血者头像"
            },
            {
                "privacy_key": "AlbumWriteOnly",
                "privacy_text": "保存图片功能"
            }
        ],
        "privacy_ver": 2
    }
    return sendHttps(urlName, "POST", body)

# 申请地理位置接口
def applyPrivacyInterface(appId):
    urlName = f"/wxSetPrivacy/applyPrivacyInterface/{appId}";
    body = {
        "api_name": "wx.getLocation",
        "content": "获取位置信息来定位当前所在城市，自动匹配对应血站；以及计算与各个献血点的距离、献血点导航等"
    }
    return sendHttps(urlName, "POST", body)


# 调用示例
def main():
    method_mapping = {
        "getPrivacySetting": [lambda: getPrivacySetting(appId), '查询小程序用户隐私保护指引'],
        "setting": [lambda: setting(appId), '配置小程序用户隐私保护指引'],
        
        "getCodePrivacyInfo": [lambda: getCodePrivacyInfo(appId), '检测隐私接口权限'],
        "applyPrivacyInterface": [lambda: applyPrivacyInterface(appId), '申请地理位置接口'],
        
        "codeCommit": [lambda: codeCommit(appId), '小程序提交代码'],
        "submitAudit": [lambda: submitAudit(appId), '小程序提交审核'],
        "getLastAuditStatus": [lambda: getLastAuditStatus(appId), '查询最新一次提交的审核状态'],
        "release": [lambda: release(appId), '小程序发布版本']
        # 在这里添加更多的命令与方法映射
    }
    
    appId = input("首次启动请输入appId（或输入 'exit' 退出程序）: ")
    if(appId == "exit"):
        print("程序退出")
        return
    
    while True:
        # 等待用户输入命令
        print("可用命令列表:")
        for command in method_mapping:
            print(f"{command} - {method_mapping[command][1]}")
        command = input("\n请输入命令（或输入 'exit' 退出程序）: ")
        
        if command == "exit":
            print("程序退出")
            break
        elif "appId=" in command:
            # 检查命令中是否包含"appId="，并提取出appId的值
            parts = command.split("appId=")
            if len(parts) == 2:
                appId = parts[1]
                print(f"已更新app_id为: {appId}")
            else:
                print("命令格式不正确")
        elif command in method_mapping:
            # 根据命令调用相应的方法
            result = method_mapping[command][0]()
            print(result)
        else:
            print("未知命令，请重新输入")

if __name__ == "__main__":
    main()